package service.credit.impl;

import java.util.ArrayList;
import java.util.List;

import model.company.Companyassetsdebt;
import model.company.Companybasic;
import model.company.Companycashflow;
import model.company.Companycontact;
import model.company.Companymanage;
import model.company.Companyprofit;
import model.company.Companytopmanager;
import model.grant.Customgrant;

import dao.CompanybasicDao;
import dao.CompanycontactDao;
import dao.CompanymanageDao;
import dao.CustomgrantDao;
import dao.company.CompanyassetsdebtDao;
import dao.company.CompanycashflowDao;
import dao.company.CompanyprofitDao;
import dao.company.CompanytopmanagerDao;
import exception.Exceptions;

import service.credit.EnterpriseSearchService;
import vo.EnterpriseInforBean;

public class EnterpriseSearchServiceImpl implements EnterpriseSearchService {
	private CompanybasicDao companybasicDao;
	private CompanymanageDao companymanageDao;
	private CompanycontactDao companycontactDao;
	private CompanytopmanagerDao companytopmanagerDao;
	private CustomgrantDao customgrantDao;
	
	private CompanyassetsdebtDao companyassetsdebtDao; //资产负债表
	private CompanycashflowDao companycashflowDao; //现金流量
	private CompanyprofitDao companyprofitDao;  //损益表

	@Override
	public List<?> get_enterpriseList() {
		List<Companybasic> _companybasic = this.companybasicDao.findAll();
		List<EnterpriseInforBean> enterpriseInforBeanList = new ArrayList<EnterpriseInforBean>();
		if (_companybasic != null) {
			for (Companybasic companybasic : _companybasic) {
				EnterpriseInforBean _enterpriseInforBean = new EnterpriseInforBean();
				if (companybasic.getId() != null) {
					Companytopmanager _companytopmanager = companytopmanagerDao
							.findByOne("cid = " + companybasic.getId()
									+ " and category = 1");
					if (_companytopmanager != null) {
						_enterpriseInforBean.setLawPeople(_companytopmanager
								.getName());
					}
				}
				Companycontact companycontact = this.companycontactDao.findOne(
						"id", companybasic.getId());
				if (companycontact != null) {
					Companymanage companymanage = this.companymanageDao
							.findOne("id", companycontact.getId());
					if (companymanage != null) {
						_enterpriseInforBean.setMainproducts(companymanage
								.getMainproducts());
					}
					_enterpriseInforBean.setTelephone(companycontact
							.getTelephone());
					_enterpriseInforBean.setMailingaddress(companycontact
							.getMailingaddress());
					_enterpriseInforBean
							.setLinkman(companycontact.getLinkman());
				}
				_enterpriseInforBean.setName(companybasic.getName());
				_enterpriseInforBean.setId(companybasic.getId());
				enterpriseInforBeanList.add(_enterpriseInforBean);
			}
		}
		return enterpriseInforBeanList;
	}

	@Override
	public List<EnterpriseInforBean> searchEnterpriseByID(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EnterpriseInforBean> searchEnterpriseByName(String name) {
		Companybasic _companybasic = this.companybasicDao.findOne("name", name);
		Companytopmanager _companytopmanager = companytopmanagerDao
				.findByOne("cid = " + _companybasic.getId()
						+ " and category = 1");

		EnterpriseInforBean enterpriseInforBean = new EnterpriseInforBean();
		if (_companybasic != null) {
			enterpriseInforBean.setName(_companybasic.getName());
			Companycontact companycontact = this.companycontactDao.findOne(
					"id", _companybasic.getId());
			if (companycontact != null) {
				Companymanage companymanage = this.companymanageDao.findOne(
						"id", companycontact.getId());
				if (companymanage != null) {
					enterpriseInforBean.setMainproducts(companymanage
							.getMainproducts());
				}
				enterpriseInforBean.setTelephone(companycontact.getTelephone());
				enterpriseInforBean.setMailingaddress(companycontact
						.getMailingaddress());
				enterpriseInforBean.setLinkman(companycontact.getLinkman());
			}
			if (_companytopmanager != null) {
				enterpriseInforBean.setLawPeople(_companytopmanager.getName());
				;
			}
			enterpriseInforBean.setId(_companybasic.getId());

		}
		List<EnterpriseInforBean> enterpriseInforBeanList = new ArrayList<EnterpriseInforBean>();
		enterpriseInforBeanList.add(enterpriseInforBean);
		return enterpriseInforBeanList;
	}

	@Override
	public Customgrant getCustomgrantByCustomId(Integer customId)
			throws Exceptions {
		if (this.customgrantDao.findByOne("customid = " + customId
				+ " and customtype = 2") != null) {
			return this.customgrantDao.findByOne("customid = " + customId
					+ " and customtype = 2");
		} else {
			throw new Exceptions("对不起，该客户尚无授信记录。不能进行此操作！");
		}
	}

	@Override
	public EnterpriseInforBean searchOneEnterprise(Integer id) throws Exceptions {
		Companybasic _companybasic = this.companybasicDao.findOne("id", id);
		Companytopmanager _companytopmanager = companytopmanagerDao
				.findByOne("cid = " + id + " and category = 1");
		
		List<Companyassetsdebt> companyassetsdebts = this.companyassetsdebtDao.findModel("cid = " + id + " order by decdate");
		List<Companycashflow> companycashflows = this.companycashflowDao.findModel("cid = " + id + " order by decdate");
		List<Companyprofit> companyprofits = this.companyprofitDao.findModel("cid = " + id + " order by decdate");
		//private CompanyprofitDao companyprofitDao;  //损益表
		Companyassetsdebt companyassetsdebt = (companyassetsdebts == null) ? null : companyassetsdebts.get(0);
		Companycashflow companycashflow = companycashflows == null ? null : companycashflows.get(0);
		Companyprofit companyprofit = companyprofits == null ? null : companyprofits.get(0);
		
		EnterpriseInforBean enterpriseInforBean = new EnterpriseInforBean();
		
		if(companyassetsdebt != null){
			try{
				enterpriseInforBean.setAllMoney(companyassetsdebt.getTotalassets());
			}catch(Exception e){
				enterpriseInforBean.setAllMoney(0.0);
			}
			
			try{
				enterpriseInforBean.setTotalOwnMoney(companyassetsdebt.getOsequitytogether());
			}catch(Exception e){
				enterpriseInforBean.setTotalOwnMoney(0.0);
			}
			
			try{
				Double dd = companyassetsdebt.getTotalliabilities();//负债合计
				Double cc = companyassetsdebt.getTotalassets();//资产总计;
				
				if(cc != null && cc != 0.0){
					Double rate = dd / cc * 100;
					enterpriseInforBean.setAssetLiabilityRatio((double)Math.round(rate*100)/100);
				}
				
			}catch(Exception e){
				enterpriseInforBean.setAssetLiabilityRatio(0.0);
			}
			
		}
		if(companycashflow != null){
			enterpriseInforBean.setSalesRevenue(companycashflow.getGoodserviceinc());
		}
		if(companyprofit != null){
			enterpriseInforBean.setProfits(companyprofit.getTpf());
			enterpriseInforBean.setTotalValue(companyprofit.getMainbusinessincome());
		}

		if (_companybasic != null) {
			enterpriseInforBean.setName(_companybasic.getName());
			enterpriseInforBean.setCreditcode(_companybasic.getCreditcode());
			Companycontact companycontact = this.companycontactDao.findOne(
					"cid", _companybasic.getId());
			if (companycontact != null) {
				Companymanage companymanage = this.companymanageDao.findOne(
						"id", companycontact.getCid());
				if (companymanage != null) {
					enterpriseInforBean.setMainproducts(companymanage
							.getMainproducts());
				}
				enterpriseInforBean.setTelephone(companycontact.getTelephone());
				enterpriseInforBean.setMailingaddress(companycontact
						.getMailingaddress());
				enterpriseInforBean.setLinkman(companycontact.getLinkman());
			}else{
				throw new Exceptions("请完善企业的法人信息！");
			}
			if (_companytopmanager != null) {
				enterpriseInforBean.setLawPeople(_companytopmanager.getName());
			}else{
				throw new Exceptions("请完善企业的高管信息！");
			}
			enterpriseInforBean.setId(_companybasic.getId());
		}else{
			throw new Exceptions("请完善企业的基本信息！");
		}
		
		return enterpriseInforBean;
	}

	public CompanybasicDao getCompanybasicDao() {
		return companybasicDao;
	}

	public void setCompanybasicDao(CompanybasicDao companybasicDao) {
		this.companybasicDao = companybasicDao;
	}

	public CompanymanageDao getCompanymanageDao() {
		return companymanageDao;
	}

	public void setCompanymanageDao(CompanymanageDao companymanageDao) {
		this.companymanageDao = companymanageDao;
	}

	public CompanycontactDao getCompanycontactDao() {
		return companycontactDao;
	}

	public void setCompanycontactDao(CompanycontactDao companycontactDao) {
		this.companycontactDao = companycontactDao;
	}

	public CompanytopmanagerDao getCompanytopmanagerDao() {
		return companytopmanagerDao;
	}

	public void setCompanytopmanagerDao(
			CompanytopmanagerDao companytopmanagerDao) {
		this.companytopmanagerDao = companytopmanagerDao;
	}

	public CustomgrantDao getCustomgrantDao() {
		return customgrantDao;
	}

	public void setCustomgrantDao(CustomgrantDao customgrantDao) {
		this.customgrantDao = customgrantDao;
	}

	public CompanyassetsdebtDao getCompanyassetsdebtDao() {
		return companyassetsdebtDao;
	}

	public void setCompanyassetsdebtDao(CompanyassetsdebtDao companyassetsdebtDao) {
		this.companyassetsdebtDao = companyassetsdebtDao;
	}

	public CompanycashflowDao getCompanycashflowDao() {
		return companycashflowDao;
	}

	public void setCompanycashflowDao(CompanycashflowDao companycashflowDao) {
		this.companycashflowDao = companycashflowDao;
	}

	public CompanyprofitDao getCompanyprofitDao() {
		return companyprofitDao;
	}

	public void setCompanyprofitDao(CompanyprofitDao companyprofitDao) {
		this.companyprofitDao = companyprofitDao;
	}
}
